package com.sihai.ehr.web.action.exam;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.ExamBaseConfig;
import com.sihai.ehr.model.biz.ExamQuanSet;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.service.ExamBaseConfigService;
import com.sihai.ehr.service.ExamItemSetService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.ExamBaseConfigPojo;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

/**
 * @author wxn
 * @since 12-10-09 21:58
 * 考核指标管理
 */
@Results({
        @Result(name = "list", location = "exam_base_config_list.jsp"),
        @Result(name = "add", location = "exam_base_config_add.jsp"),
        @Result(name = "main", location = "exam_base_config_main.jsp")
})
public class ExamBaseConfigAction extends EHRAction {
    private List<Organization> organizations;

    @BeanInject
    private DutyService dutyService;
    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private ExamBaseConfigService examBaseConfigService;
    @BeanInject
    private ExamItemSetService examItemSetService;

    private LimitedList<ExamBaseConfigPojo> examBaseConfigPojos;

    private ExamBaseConfig examBaseConfig;

    private String batchIds;
    private String batchType;

    private String createPageTitle;

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7204414972672241268L;

    @Action("/exam/base_config_main")
    @Override
    public String execute() throws Exception {
        listAction = "/exam/base_config_list.xhtml";
        listTitle = "考核指标设定";
        showChildOrg = true;
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        return "main";
    }

    //考核指标设定-列表
    @Action("/exam/base_config_list")
    public String list() throws Exception {

        examBaseConfigPojos = examBaseConfigService.findByTopOrganization(organizationId, NumberUtils.createLong(getParameter("dutyId")), createLimit(), createSorter());

        /* 分页 */
        this.paging(examBaseConfigPojos);
        /* 设置翻页action */
        this.setPageUrl("exam/base_config_list.xhtml");
        
        return "list";
    }

    @Action("/exam/base_config_create")
    public String create() throws Exception {
        if(examBaseConfig == null || examBaseConfig.getDuty() == null || examBaseConfig.getDuty().getId() == null){
            return execute();
        }

        Duty duty = dutyService.get(examBaseConfig.getDuty().getId());
        examBaseConfig.setDuty(duty);
        if(examBaseConfig.getId() == null){//new
            examBaseConfigService.save(examBaseConfig);
            //add by zj
            ExamQuanSet set=new ExamQuanSet();
            set.setDuty(duty);
            set.setItemCount(0);
            set.setpState(examBaseConfig.getPercentageQuantitative());
            examItemSetService.saveExamQuanSet(set);
        }else{//edit
            ExamBaseConfig config = examBaseConfigService.get(examBaseConfig.getId());
            config.setDuty(duty);
            config.setPercentageQualitative(examBaseConfig.getPercentageQualitative());
            config.setPercentageQuantitative(examBaseConfig.getPercentageQuantitative());
            config.setPercentageFinish(examBaseConfig.getPercentageFinish());
            config.setPercentagePerformance(examBaseConfig.getPercentagePerformance());
            config.setPeriodType(examBaseConfig.getPeriodType());
            config.setPeriodMode(examBaseConfig.getPeriodMode());
            examBaseConfigService.save(config);
            //add by zj
            ExamQuanSet set= examItemSetService.findExamQuanSetByDutyId(duty.getId());
            if(set == null){
                set = new ExamQuanSet();
                set.setDuty(duty);
                set.setItemCount(0);
            }
            set.setpState(examBaseConfig.getPercentageQuantitative());
            examItemSetService.saveExamQuanSet(set);
        }
        return execute();
    }

    //跳转到添加/编辑页面
    @Action("/exam/base_config_add")
    public String add() throws Exception {
        if(examBaseConfig != null){
            if(examBaseConfig.getId() != null){//edit
                createPageTitle = "修改";
                examBaseConfig = examBaseConfigService.get(examBaseConfig.getId());
            }else if(examBaseConfig.getDuty() != null && examBaseConfig.getDuty().getId() != null){//create
                createPageTitle = "新增";
                examBaseConfig.setDuty(dutyService.get(examBaseConfig.getDuty().getId()));
            }else{
                return execute();
            }
        }
        return  "add";
    }

    //跳转到添加/编辑页面(批量添加修改)
    @Action("/exam/base_config_batch_add")
    public String batchAdd() throws Exception {
        if("edit".equals(batchType)){//批量修改
            createPageTitle = "批量修改";
            //获得第一条记录的数据
            examBaseConfig = examBaseConfigService.get(examBaseConfig.getId());
        }else if("create".equals(batchType)){//批量添加
            createPageTitle = "批量添加";
        }
        return  "add";
    }

    @Action("/exam/base_config_batch_create")
    public String batchCreate() throws Exception {
        if(examBaseConfig == null){
            return execute();
        }

        examBaseConfigService.saveForBatch(examBaseConfig, batchType, batchIds);

        return execute();
    }

    @Action("/exam/base_config_delete")
    public String remove() throws Exception {
        return execute();
    }


    public List<Organization> getOrganizations() {
        return organizations;
    }

    public LimitedList<ExamBaseConfigPojo> getExamBaseConfigPojos() {
        return examBaseConfigPojos;
    }

    public ExamBaseConfig getExamBaseConfig() {
        return examBaseConfig;
    }

    public void setExamBaseConfig(ExamBaseConfig examBaseConfig) {
        this.examBaseConfig = examBaseConfig;
    }

    public String getBatchIds() {
        return batchIds;
    }

    public void setBatchIds(String batchIds) {
        this.batchIds = batchIds;
    }

    public String getBatchType() {
        return batchType;
    }

    public void setBatchType(String batchType) {
        this.batchType = batchType;
    }

    public String getCreatePageTitle() {
        return createPageTitle;
    }
}
